package cs4280.ass2.action;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ScheduledExecutorService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;

import cs4280.ass2.entity.House;
import cs4280.ass2.entity.Member;
import cs4280.ass2.entity.Schedule;
import cs4280.ass2.entity.ScheduleSeatDetail;
import cs4280.ass2.entity.ScheduleTO;
import cs4280.ass2.entity.Ticket;
import cs4280.ass2.entity.User;
import cs4280.ass2.persistence.HouseDao;
import cs4280.ass2.persistence.MemberDao;
import cs4280.ass2.persistence.ScheduleDao;
import cs4280.ass2.persistence.ScheduleSeatDetailDao;
import cs4280.ass2.persistence.TicketDao;
import cs4280.ass2.util.httpUtils;

public class PaymentAction extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String scheduleIdStr = req.getParameter("scheduleId");

		if (NumberUtils.isDigits(scheduleIdStr)) {
			long scheduleId = NumberUtils.toLong(scheduleIdStr);
			ScheduleTO scheduleTO = ScheduleDao.getInstance()
					.retrieveScheduleTOById(scheduleId);
			List<ScheduleSeatDetail> seatDetailList = ScheduleSeatDetailDao
					.getInstance().retrieveSeatListByScheduleAndSessionId(
							scheduleId, req.getSession().getId());

			double loyalityEarned = scheduleTO.getPrice()
					* seatDetailList.size();
			req.setAttribute("loyalityEarned", loyalityEarned);
			req.setAttribute("seatDetailList", seatDetailList);
			req.setAttribute("scheduleTO", scheduleTO);
			getServletContext().getRequestDispatcher("/payment.jsp").forward(
					req, resp);
		} else {
			resp.sendError(HttpServletResponse.SC_BAD_REQUEST,
					"invalid scheduleId");
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String scheduleIdStr = req.getParameter("scheduleId");
		if (NumberUtils.isDigits(scheduleIdStr)) {
			HttpSession session = req.getSession();
			String sessionId = session.getId();
			long userId = httpUtils.getLoginUserId(req);
			long scheduleId = NumberUtils.toLong(scheduleIdStr);

			List<ScheduleSeatDetail> seatDetailList = ScheduleSeatDetailDao
					.getInstance().retrieveSeatListByScheduleAndSessionId(
							scheduleId, sessionId);

			double loyaltyPointUsed = 0;
			if (httpUtils.isLogined(req)) {
				Member member = (Member) httpUtils.getLoginUser(req);
				loyaltyPointUsed = NumberUtils.toDouble(
						req.getParameter("loyaltyPoint"), 0);
				double loyaltyPointEarn = NumberUtils.toDouble(
						req.getParameter("loyaltyPointEarn"), 0);
				MemberDao.getInstance().updateLoyalityPoint(member.getId(),
						member.getLoyaltyPoint() - loyaltyPointUsed+loyaltyPointEarn ,
						httpUtils.getLoginUserId(req));
				session.setAttribute("loggedInUser", MemberDao.getInstance().retrieveMember(member.getId()));
			}

			for (ScheduleSeatDetail seat : seatDetailList) {
				Ticket t = new Ticket();
				t.setAppUserId(userId);
				t.setCreateDate(new Date());
				t.setCreateUser(userId);
				t.setModifyDate(new Date());
				t.setModifyUser(userId);
				t.setScheduleSeatDetailId(seat.getId());
				t.setTicketStatus((loyaltyPointUsed > 0) ? Ticket.TICKET_STATUS_NON_REFUNDABLE
						: Ticket.TICKET_STATUS_ACTIVE);
				TicketDao.getInstance().addTicket(t);
				ScheduleSeatDetailDao.getInstance().updateSeatStatus(
						seat.getId(), ScheduleSeatDetail.SEAT_STATUS_SOLD,
						sessionId, userId);
			}

			resp.sendRedirect("thanksyou.jsp");
		} else {
			resp.sendError(HttpServletResponse.SC_BAD_REQUEST,
					"invalid scheduleId");
		}
	}
}
